﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Crossroad.Utils.Models;

namespace Crossroad.Utils
{
    /// <summary>
    /// 所有对象的辅助方法
    /// </summary>
    public class ObjectUtil
    {

        /// <summary>
        /// 枚举结构转换字典列表
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <returns></returns>
        public static List<DicDom> GetDicList<TEnum>() where TEnum : struct
        {
            var type = typeof(TEnum);
            var target = Activator.CreateInstance<TEnum>();
            var list = (from field in type.GetFields()
                        where field.IsStatic
                        let attrs = field.GetCustomAttributes(typeof(DescriptionAttribute), false)
                        let attr = attrs.First() as DescriptionAttribute
                        select new DicDom { label = attr.Description, value = field.GetValue(target) }
            ).ToList();

            return list;
        }


    }
}
